import csv
import re
import requests

url = 'https://movie.douban.com/top250'
headers_dict = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62 "
}
data = requests.get(url, headers=headers_dict)
DataText = data.text

obj = re.compile(r'<li>.*?<div class="item">.*?<span class="title">(?P<name>.*?)</span>'
                 r'.*?<p class="">.*?<br>(?P<year>.*?)&nbsp'
                 r'.*?<div class="star">.*?<span>(?P<commit>.*?)</span>'
                 r'.*?<span class="inq">(?P<jj>.*?)</span>', re.S)

f = open("data.csv", mode="w", encoding="utf-8")
DataWriter =csv.writer(f)
for it in obj.finditer(DataText):
    #print(it.group("name"))
    #print(it.group("year").strip())
    #print(it.group("commit"))
    #print(it.group("jj"))
    dic =it.groupdict()
    dic['year'] =dic['year'].strip()
    DataWriter.writerow(dic.values())

f.close()
print("OK")
